package com.sunny.ynhz.ui.patrol.problems.draft;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sunny.ynhz.R;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luao on 2017/7/24.
 */

public class DraftActivity extends AppBaseTranslationTitleBarActivity implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.fragments)
    FrameLayout fragments;
    private Fragment nowFragment1;

    @BindView(R.id.radio)
    RadioGroup titleRadioGroup;
    @BindView(R.id.patrol_problem)
    RadioButton patrolProblem;
    @BindView(R.id.other_problem)
    RadioButton otherProblem;
    @BindView(R.id.more_style)
    RadioButton more_style;
    private PatrolDraftFragment patrolDraftFragment;
    private ProblemDraftFragment problemDraftFragment;
    private MoreStyleDraftFragment moreStyleFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draft);
        ButterKnife.bind(this);
        titleRadioGroup.setVisibility(View.VISIBLE);
        titleRadioGroup.setOnCheckedChangeListener(this);
        patrolProblem.setText(Html.fromHtml("&#160;&#160;问题&#160;&#160;"));
        otherProblem.setText(Html.fromHtml("&#160;&#160;巡河&#160;&#160;"));
        more_style.setText("多样化");
        patrolDraftFragment = PatrolDraftFragment.getInstance();
        problemDraftFragment = ProblemDraftFragment.getInstance();
        moreStyleFragment = MoreStyleDraftFragment.getInstance();
        swithFragment(problemDraftFragment, null);
        more_style.setVisibility(View.GONE);
//        more_style.post(new Runnable() {
//            @Override
//            public void run() {
//                int measuredWidth = more_style.getMeasuredWidth();
//                ViewGroup.LayoutParams layoutParams = otherProblem.getLayoutParams();
//                layoutParams.width=measuredWidth;
//                otherProblem.requestLayout();
//                ViewGroup.LayoutParams layoutParams1 = patrolProblem.getLayoutParams();
//                layoutParams1.width=measuredWidth;
//                patrolProblem.requestLayout();
//            }
//        });

    }


    /**
     * @param nowFragment  当前的fragment
     * @param nextFragment 即将切换的fragment
     */
    public void swithFragment(Fragment nowFragment, Fragment nextFragment) {
        nowFragment1 = nextFragment;
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if (nextFragment == null) {
            fragmentTransaction.add(R.id.fragments, nowFragment).commit();
            nowFragment1 = nowFragment;
        } else {
            if (nextFragment.isAdded()) {
                fragmentTransaction.hide(nowFragment).show(nextFragment).commit();
            } else {
                fragmentTransaction.hide(nowFragment).add(R.id.fragments, nextFragment).commit();
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.patrol_problem:
                swithFragment(nowFragment1, problemDraftFragment);
                break;
            case R.id.other_problem:
                swithFragment(nowFragment1, patrolDraftFragment);
                break;
            case R.id.more_style:
                swithFragment(nowFragment1, moreStyleFragment);

        }

    }

}
